/*设计实现双端队列。你的实现需要支持以下操作：
* MyCircularDeque(k)：构造函数,双端队列的大小为k。
* insertFront()：将一个元素添加到双端队列头部。 如果操作成功返回 true。
* insertLast()：将一个元素添加到双端队列尾部。如果操作成功返回 true。
* deleteFront()：从双端队列头部删除一个元素。 如果操作成功返回 true。
* deleteLast()：从双端队列尾部删除一个元素。如果操作成功返回 true。
* getFront()：从双端队列头部获得一个元素。如果双端队列为空，返回 -1。
* getRear()：获得双端队列的最后一个元素。如果双端队列为空，返回 -1。
* isEmpty()：检查双端队列是否为空。
* isFull()：检查双端队列是否满了。
* */

package newcoder.Queue_4;

import java.util.Arrays;

class MyCircularDeque{

    private int[] deque;
    private int count;
    private int headIndex;
    private int rearIndex;
    public MyCircularDeque(int k){

        this.deque = new int[k];
        this.count = 0;
        this.headIndex = 0;
        this.rearIndex = k - 1;
    }

    public boolean insertFront(int value){
        if (this.count == this.deque.length) return false;
        headIndex = (this.headIndex - 1 + deque.length) % deque.length;
        this.deque[headIndex] = value;
        this.count++;
        return true;
    }

    public boolean insertLast(int value){
        if (this.count == this.deque.length) return false;
        rearIndex = (this.rearIndex + 1 + this.deque.length) % this.deque.length;
        this.deque[rearIndex] = value;
        this.count++;
        return true;
    }

    public boolean deleteFront(){

        if (count == 0) return false;
        this.headIndex = (this.headIndex + 1) % this.deque.length;
        this.count--;
        return true;
    }

    public boolean deleteLast(){
        if (count == 0) return false;
        this.rearIndex = (this.rearIndex - 1) % this.deque.length;
        this.count--;
        return true;
    }

    public int getFront(){
        if (this.count == 0) return -1;
        return this.deque[this.headIndex];
    }

    public int getRear(){
        if (this.count == 0) return -1;
        return this.deque[(this.headIndex + this.count - 1) % this.deque.length];
    }

    public boolean isEmpty(){
        return this.count == 0;
    }

    public boolean isFull(){
        return this.count == this.deque.length;
    }
}

public class Leetcode641 {

    public static void main(String[] args) {

        MyCircularDeque obj = new MyCircularDeque(3);
        String[] str = new String[]{"MyCircularDeque","insertLast","insertLast","insertFront",
                "insertFront","getRear","isFull","deleteLast","insertFront","getFront"};
        int[][] nums = new int[][]{{3},{1},{2},{3},{4},{},{},{},{4},{}};
        String[] resv = new String[str.length];
        for (int i = 0;i < str.length;i++){
            if (str[i] == "MyCircularDeque"){
                resv[i] = "null";
            }
            if (str[i] == "insertLast"){
                resv[i] = obj.insertLast(nums[i][0]) + "";
            }
            if (str[i] == "insertFront"){
                resv[i] = obj.insertFront(nums[i][0]) + "";
            }
            if (str[i] == "deleteLast"){
                resv[i] = obj.deleteLast() + "";
            }
            if (str[i] == "deleteFront"){
                resv[i] = obj.deleteFront() + "";
            }
            if (str[i] == "getRear"){
                resv[i] = obj.getRear() + "";
            }
            if (str[i] == "getFront"){
                resv[i] = obj.getFront() + "";
            }
            if (str[i] == "isEmpty"){
                resv[i] = obj.isEmpty() + "";
            }
            if (str[i] == "isFull"){
                resv[i] = obj.isFull() + "";
            }
        }
        System.out.println(Arrays.toString(resv));
    }
}
